package ebuy.com.cotroller;

import ebuy.com.pojo.Easybuy_user;
import ebuy.com.server.Easybuy_userserver;
import ebuy.com.util.RespBean;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.*;

import java.util.Objects;

@RestController
public class EasyBuy_userCotroller {
    @Autowired
    private Easybuy_userserver easybuy_userserver;
    @RequestMapping("/lohin")
    public RespBean Login(@RequestParam("usaname") String usaname, @RequestParam("password") String password){
        Easybuy_user loginrequest = easybuy_userserver.Loginrequest(usaname, password);
        if (Objects.isNull(loginrequest)){
           return RespBean.failure("账号或密码错误");
        }
        return RespBean.success("登入成功",loginrequest.getUserName());
    }
    @GetMapping("/address")
    public  RespBean Checkpersonaladdress(@RequestParam("longname") String longname){
        //查询默认地址
        Easybuy_user lookfortheaddress = easybuy_userserver.lookfortheaddress(longname,1);
        return RespBean.success("查询成功",lookfortheaddress);

    }
    @PostMapping("/registered")
    public RespBean getregistereduser(Easybuy_user easybuy_user){
        String [] arter=new String[]{"https://img1.baidu.com/it/u=2062164223,3783917881&fm=26&fmt=auto&gp=0.jpg","https://img1.baidu.com/it/u=1851283359,3457678391&fm=26&fmt=auto&gp=0.jpg","https://img2.baidu.com/it/u=2590128318,632998727&fm=26&fmt=auto&gp=0.jpg"};
         int sum= (int) (Math.random()*arter.length);//随机给一个头像 可以从数据库里面查询
      String cc=easybuy_user.getLoginName();
      easybuy_user.setUserName(cc);
        easybuy_user.setAvar(arter[sum]);
        int increaseuser = easybuy_userserver.increaseuser(easybuy_user);
        return  RespBean.success("增加成功");
    }
    //判断用户名字是不是重复
    @GetMapping("/kkkk")
    public RespBean Judgewhetherheusername(@RequestParam(value = "logname") String logname){
        Easybuy_user judgewhethertheusersnamesrepeated = easybuy_userserver.Judgewhethertheusersnamesrepeated(logname);
        if (judgewhethertheusersnamesrepeated!=null){
            return RespBean.success("名字重复了");
        }
        return RespBean.success(null);
    }
    @GetMapping("/getusw")
    public RespBean GetUser(@RequestParam(value = "logname") String logname){
        Easybuy_user judgewhethertheusersnamesrepeated = easybuy_userserver.Judgewhethertheusersnamesrepeated(logname);

        return RespBean.success("查询成功",judgewhethertheusersnamesrepeated);
    }
}

